//
//  1-02.c
//  LearningOpenGL
//
//  Created by wangrui on 14-7-4.
//  Copyright (c) 2014年 PPS. All rights reserved.
//

/*
 *buffer 的使用，但是暂时没有图形效果
 */

#include <OpenGL/OpenGL.h>
#include <OpenGL/gl3.h>
#include <GLUT/GLUT.h>
#include <stdio.h>
#include <math.h>

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated" //消除 ‘废弃的api’ 警告，GLUT已全部在Mac上废除，可用glfw替换


#define VERTICES 0
#define INDICES 1
#define NUM_BUFFERS 2
#define BUFFER_OFFSET(bytes) ((GLubyte *) NULL + (bytes))

GLfloat vertices[] = {
	-5.0,-5.0,-0.0,
	 5.0,-5.0,-0.0,
	 5.0, 5.0,-0.0,
	-5.0, 5.0,-0.0,
	-1.0,-1.0, 1.0,
	 1.0,-1.0, 1.0,
	1.0, 1.0, 1.0,
	-1.0, 1.0, 1.0
};

GLubyte indices[][4] = {
	{0,1,2,3},
	{4,7,6,5},
	{0,4,5,1},
	{3,2,6,7},
	{0,3,7,4},
	{1,5,6,2}
};

void display()
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0, 1.0, 1.0);
	
	GLuint buffers[] = {};
	glGenBuffers(NUM_BUFFERS, buffers);
	
	glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
	
	glEnableClientState(GL_VERTEX_ARRAY);
	glVertexPointer(3, GL_FLOAT, 0, vertices);
	
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[INDICES]);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
	glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
	
	glFlush();
}

int main(int argc, char **argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(300, 300);
	glutCreateWindow(*argv);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
	
	glutDisplayFunc(display);

	glutMainLoop();
	
	return 0;
}

#pragma clang diagnostic pop

